package cn.iocoder.yudao.module.dating.controller.admin.smallNote;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dating.controller.admin.smallNote.vo.*;
import cn.iocoder.yudao.module.dating.dal.dataobject.smallNote.SmallNoteDO;
import cn.iocoder.yudao.module.dating.service.smallNote.SmallNoteService;

@Tag(name = "管理后台 - 小纸条")
@RestController
@RequestMapping("/dating/small-note")
@Validated
public class SmallNoteController {

    @Resource
    private SmallNoteService smallNoteService;

    @PostMapping("/create")
    @Operation(summary = "创建小纸条")
    @PreAuthorize("@ss.hasPermission('dating:small-note:create')")
    public CommonResult<Long> createSmallNote(@Valid @RequestBody SmallNoteSaveReqVO createReqVO) {
        return success(smallNoteService.createSmallNote(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小纸条")
    @PreAuthorize("@ss.hasPermission('dating:small-note:update')")
    public CommonResult<Boolean> updateSmallNote(@Valid @RequestBody SmallNoteSaveReqVO updateReqVO) {
        smallNoteService.updateSmallNote(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小纸条")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dating:small-note:delete')")
    public CommonResult<Boolean> deleteSmallNote(@RequestParam("id") Long id) {
        smallNoteService.deleteSmallNote(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小纸条")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dating:small-note:query')")
    public CommonResult<SmallNoteRespVO> getSmallNote(@RequestParam("id") Long id) {
        SmallNoteDO smallNote = smallNoteService.getSmallNote(id);
        return success(BeanUtils.toBean(smallNote, SmallNoteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小纸条分页")
    @PreAuthorize("@ss.hasPermission('dating:small-note:query')")
    public CommonResult<PageResult<SmallNoteRespVO>> getSmallNotePage(@Valid SmallNotePageReqVO pageReqVO) {
        PageResult<SmallNoteDO> pageResult = smallNoteService.getSmallNotePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmallNoteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小纸条 Excel")
    @PreAuthorize("@ss.hasPermission('dating:small-note:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSmallNoteExcel(@Valid SmallNotePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmallNoteDO> list = smallNoteService.getSmallNotePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "小纸条.xls", "数据", SmallNoteRespVO.class,
                        BeanUtils.toBean(list, SmallNoteRespVO.class));
    }

}